//package edu.siu.math.egut.main;
//
//import java.io.File;
//import java.io.FileNotFoundException;
//import java.io.FileWriter;
//import java.io.IOException;
//import java.util.Arrays;
//import java.util.Calendar;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.Scanner;
//
//import org.apache.commons.collections15.list.TreeList;
//
//import edu.siu.math.egut.io.Algorithms;
//import edu.siu.math.egut.io.SectionIntegralPiece;
//import edu.siu.math.egut.util.BadSubstitutionException;
//import edu.siu.math.egut.util.Group;
//import edu.siu.math.egut.util.Monomial;
//import edu.siu.math.egut.util.NotARootException;
//import edu.siu.math.egut.util.Polynomial;
//import edu.siu.math.egut.util.PolynomialReadException;
//import edu.siu.math.egut.util.RationalFunction;
//import edu.siu.math.egut.util.Sunipotent;
//import edu.siu.math.egut.util.Toral;
//import edu.siu.math.egut.util.Unipotent;
//
///**
// * Facilitates computation of the integral of the  unramified vector 
// * in an induced  representation over a unipotent group against a 
// * character or more general "psi factor."  
// * 
// * @author Joseph Hundley
// *
// */
//
//// TODO:  add auto-kill, check piece management carefully.
//public class OldSectionIntegral {
//
//    
//    private static final String NEGATIVE_EXPONENT_WARNING = "WARNING: One or" +
//    		" more of the Sunipotents contains a negative exponent.\n" +
//    		"It is advisable to use substitutions to eliminate" +
//    		" these negative exponents.  Otherwise, be very careful\n" +
//    		"with substitutions.";
//    private static final char[] SETUP_OPTIONS = {'R','O','r','o'};
//    private static final char[] MAIN_MENU_OPTIONS = {'D','d','F','f','W','w','I','R','C','S','V','E','N','P','A','L','Q','i','r','c','s','v','e','n','p','a','l','q'};
//    private static final String MAIN_MENU = "[I]wasawa \t"
//			    + "[R]e-order \t"
//			    + "[C]onjugate\t"
//			    + "[S]ubstitution\n" 
//			    +"custom [V]ariables (No z!)\t"
//			    +"act by [W]eyl elt\t"
//			    +"add/r[E]store Sunis\n"
////			    + "[N]ext Piece\t"
////			    + "Switch [P]iece\t"
//			    + "psi [F]actor\t"
//			    + "Save [A]s \t" 
//			    + "[L]oad .ser\t" +
//			    		"[D]rop last\t" 
//			    /*+ "[K]ill\t"*/
//			    + "[Q]uit\n" + "";
//    private static final String UPDATE_MESSAGE = null;
////    private static List<String> pieces = new TreeList<String>();
//    private static int rank = 1;
//    private static Group g;
////    private static String currentPiece;
//    private static String name;
////    private static File directory;
////    private static FileWriter summary;
////    private static FileWriter texFile;
//    private static Unipotent u;
//    private static Polynomial p;
//    private static int[][] leftInv;
//    private static SectionIntegralPiece thisPiece;
//    
//
// 
//    /**
//     * @param args not used at this time TODO include option to 
//     * have setup given in args
//     * @throws BadSubstitutionException if an attempt is made to substitute
//     * something for a variable which appears with a negative exponent.
//     */
//    public static void main(String[] args) throws BadSubstitutionException {
//
//
//	Scanner scanner = new Scanner(System.in);
//	thisPiece = getSetUp(scanner, args);
//	p = thisPiece.getPoly();
//	u = thisPiece.getUnip();
//	leftInv = thisPiece.getLeftInv();
//	rank =leftInv[0].length;
//	g = Group.getGroup(rank);
//	try {
//	    initializeAuxFiles( scanner);
//	} catch (IOException e1) {
//	    say("Unable to initialize aux files.");
//	    e1.printStackTrace();
//	    System.exit(1);
//	}
//	
////	Piece.savePiece(name +"/"+currentPiece+".ser", thisPiece);
//
//	boolean done = false;
//	while( !done)
//	{
//	    // Show the user the unipotent, the psi factor, and his/her options
//	    System.out.println(u+"\n psi("+p+")\n\n"+ MAIN_MENU);
//	    
//	    char input =Algorithms.getMenuAnswer(MAIN_MENU_OPTIONS, scanner);
//	    if (input == 'd'|| input == 'D'){
//		u.remove(u.getLast());
//	    }
//	    else if(input == 'F' || input == 'f'){
//		say("new Factor:");
//		p = Algorithms.getPoly(scanner);
//		thisPiece.setPoly(p);
//	    } else if( input == 'W'|| input == 'w'){
//		say("Input Weyl word as multi-digit integer.");
//		int weylAsInt = Algorithms.getInt(scanner);
//		// TODO add checking
//		System.out.println( weylAsInt);
//		
//		thisPiece.actBy(weylAsInt);
//	    }
//	    else if (input == 'A'|| input == 'a')// save [A]s
//		saveAs(scanner);
//		else if (input == 'V'|| input == 'v')// 
//		{
//		    customizeVars(scanner); /* TODO syntax may need to be more complex.  think about how much needs to 
//		    be passed in order for variables to be customized correctly.... probably this is an operation on 
//		    the whole piece.*/
//		}
//		else if (input == 'E'|| input == 'e')// 
//		{
//		    
//		    System.out.println("Root:");
//		    int[] newSuniRoot = Algorithms.getRoot(scanner, g);
//		    System.out.println("Variable:");
//		    /*TODO check robustness.
//		     * add checking for problematic variables/ likely typos
//		     * add "default option" 
//		     */
//		    String var = scanner.next();
//		    u.rightMultiply(Sunipotent.create(newSuniRoot,Polynomial.create(var)));
//		    if( u.suniOf(var) == null)
//			u.associate(u.getLast(), var);
//		}
//		else if (input == 'L'|| input == 'l')// 
//		{
//		    // Load .ser
//		    System.out.println("input filename (include \".ser\")");
//		    thisPiece = Algorithms.getSavedPiece(scanner, thisPiece);
////		    currentPiece = "customLoadedAt"
////			+Calendar.getInstance().getTime().getHours()
////		    +Calendar.getInstance().getTime().getMinutes();
//		    u = thisPiece.getUnip();
//		    p = thisPiece.getPoly();
//		    leftInv = thisPiece.getLeftInv();
//		    g = u.getGroup();
//		    rank = g.getRank();
////		    pieces.add(currentPiece);
//		    /* 
//		     * add exception handling
//		     * add overwrite checking for loading a .ser file from some other directory, saving in current sessions
//		     * directory...
//		     * Check that any of this works.
//		     * make robust.
//		     */
//		    
//		    
//		}
//		else if (input == 'K'|| input == 'k')// 
//		{
//		    // KILL!TODO implement Piece.simplify()
//		    System.out.println("Not implemented yet.  Sorry.");
//		    //thisPiece.simplify();
//		    
//		}
//
//		else if (input == 'I'|| input == 'i')// do Iwasawa on rightmost
//			{
//		    update();
//		    iwasawa();
//		    /*TODO I suspect that one does not really want to 
//		     * pass 4 arguments here.  one probably wants to 
//		     * pass just one Piece, and skip this u, p 
//		     * and leftInv which might not equal the corresponding 
//		     * attributes of thisPiece.
//		     * 
//		     */
//			}//end if input calls for iwasawa
//		else if (input == 'R'|| input== 'r')// reorder
//		{
//		    
//		    
//		    boolean doneReordering = false;
//		    while (!doneReordering){
//			System.out.print(u.toStringWithIndices());
//			System.out.println("\n Which simple unipotent do you wish to move?  (Input -1 when done reordering.)");
//			int i = edu.siu.math.egut.io.Algorithms.getTermNumber(u,scanner);
//			
//			if (i < 0 || i >= u.getNumberOfTerms())
//			    doneReordering = true;
//			else {
//			    System.out
//				    .println("\n Where do you wish to move it? (Input -1 to exit reordering.)");
//			    int j = edu.siu.math.egut.io.Algorithms.getInt(scanner);
//
//				
//			    if (j < 0 || j > u.getNumberOfTerms())
//				doneReordering = true;
//			    else
//			    {
//				u.move(i, j);
//				u.cleanUp();
//				p= u.chOfVars(p);
//			    }
//			    
//			}
//			
//		    }
//		    System.out.println("Done reordering.");
//
//		} else if (input == 'c'|| input =='C')// conjugate
//		{
//		    
//		    // Get a root. 
//		    System.out.println("Input a root (as a "+ u.getGroup().getRank()+" digit integer).\n");
//		    int [] root = edu.siu.math.egut.io.Algorithms.getRoot(scanner, u.getGroup());
//		    
//		    
//		    Unipotent u1 = Unipotent.times(u, Sunipotent.create(root, Polynomial.create("z"))); 
//		    
//		    
//		    //u1.pushFarLeftAndRemove(u1.getNumberOfTerms()-1);
//		    u1 = edu.siu.math.egut.egut.Algorithms.l_reduce(u1, leftInv);
//		    Polynomial p1 = u1.chOfVars(p); // this will make the COV's in u and in p.
//		    
//		    
//		    if( u1.equals(u))
//			System.out.println(p1.minus(p)+"\n\n");
//		    else {
//			
//			System.out.println(u1+"Conjugation failed.  This would occur, e.g., if \n\tthe one-parameter subgroup\n " +
//					"corresponding to the  " +
//					"root entered does not normalize the unipotent group under consideration.\n\tthe initial unipotent contained" +
//					"a Sunipotent with a root from the list of invariant roots\n\tthe root entered corresponds to the root of " +
//					"one of the elements of this unipotent.");
//			
//		    }
//		    
//		    
//		    
//		    
//
//		} else if (input == 'S' || input == 's')// substitution
//		{
//		    /* TODO I had "add some verification?
//		     * here.  I should have put more because I no longer know what I 
//		     * meant by that.  However, I note that one might want to make 
//		     * substitutions for various reasons.  
//		     * (1) one has used conjugate to study something, and determined that the 
//		     * substitution is valid, i.e., that the part of the domain of integration
//		     *  where it does not hold 
//		     * contributes zero to the integral.
//		     * (2) one wants to break the integral into pieces and study the pieces, and the pieces 
//		     * are not the usual iwasawa pieces of some root... in this scenario 
//		     * one might also like a "save as" feature.
//		     * (3) one is making a change of variables, in this scenario, one would 
//		     * want the jacobian kept track of...
//		     *  
//		     */
//		    
//		      //get variable 
//		    System.out.println("Input a variable.");
//		    String variable = scanner.next();
//		    
//		    if(variable.charAt(0)=='1'||variable.charAt(0)=='2'||variable.charAt(0)=='0')
//		    {
//			System.out.println("Numerical input(?).  \"u\" added.");
//			variable = "u"+variable;
//		    }
//		    
//		    // get polynomial
//		    System.out.println("Input the polynomial you wish to substitute for this variable.");
//		    Polynomial q = edu.siu.math.egut.io.Algorithms.getPoly(scanner);
//		    
//		    p=p.replace(variable, q);
//		    u.replace(variable, q);
//		    u.clearZeros();
//		     
//
//		} /*else if (input == 'N' || input=='n')// done move on to next piece
//		{
//		    // write current piece in final form
//		    Piece.savePiece(name+"/"+currentPiece+".ser", thisPiece);
//		    
//		    // store location. 
//		    int index = pieces.indexOf(currentPiece);
//		    
//		     try {
//			texFile.write(thisPiece.toTex());
//		    } catch (IOException e) {
//			say("Problem writing tex file.");
//			e.printStackTrace();
//		    }
//		    
//		    // current piece is finished. delete it from the list.
//		    pieces.remove(currentPiece);
//		    
//		    // figure out what is "next."
//		    if( pieces.size() != 0 ){
//			if (index < pieces.size()) 
//			    currentPiece = pieces.get(index);
//			else 
//			    currentPiece = pieces.get(0);
//			thisPiece = Piece.loadPiece(name+"/"+currentPiece+".ser");
//			u = thisPiece.getUnip();
//			p = thisPiece.getPoly();
//			
//		    }// end if pieces size != 0...
//		    else // we're done!
//		    {
//			System.out.println("Integral completed!\n");
//			done = true;
//			
//		    }
//
//		} else if (bad syntax) // (was "D".  "D" since reclaimed for something else.  if this option is ever to be resurrected, it will need a new letter.....
//		{
//		    // write tex file
//		    try {
//			texFile.write("\\section{"+currentPiece+"}\n"+thisPiece.toTex());
//		    } catch (IOException e) {
//			say("Problem writing tex file.");
//			e.printStackTrace();
//		    }
//		    // write ser file
//		    Piece.savePiece(name+"/"+currentPiece+".ser", thisPiece);
//		    // current piece is finished. delete it from the list.
//		    pieces.remove(currentPiece);
//		    
//		    if( pieces.size()== 0 ){
//			System.out.println("\nCongratulations, you're done.\n");
//			done = true;
//		    }
//		    // TODO add feature that if there's only one left it goes to it automatically.
//		    else{
//		    System.out.println("Here is the current Piece list"+pieces+"\n\n Input your selection (Suffix only.)");
//		    	currentPiece = edu.siu.math.egut.io.Algorithms.getPieceName(scanner, name, pieces);
//		    	
//		    	// will be null if user has bailed on inputing name.
//		    	if( currentPiece != null ){
//		    	    thisPiece = Piece.loadPiece(name+"/"+currentPiece+".ser");		    	    
//		    	}
//		    }
//
//		} else if (input == 'p'|| input == 'P')// switch piece w/o marking this one
//					// done
//		{
//		    
//		    
//		   System.out.println("Save current piece?");
//		   if( Algorithms.getYesOrNo(scanner) )
//		       Piece.savePiece(currentPiece, thisPiece);
//		   
//		    
//		    System.out.println("Here is the current Piece list"+pieces+"\n\n Input your selection (Suffix only.)");
//		    	currentPiece = edu.siu.math.egut.io.Algorithms.getPieceName(scanner, name, pieces);
//		    	
//		    	// will be null if user has bailed on inputing name.
//		    	if( currentPiece != null ){
//		    	    thisPiece = Piece.loadPiece(name+"/"+currentPiece+".ser");		    	    
//		    	}
//		    
//
//		} */else if (input == 'q'|| input == 'Q')// quit
//		{
//		    
//		/*	   System.out.println("Save current piece?");
//			   if( Algorithms.getYesOrNo(scanner) )
//			       Piece.savePiece(currentPiece, thisPiece); FIXME restore(?)*/
//		    done = true;
//		     
//		} 
//	 
//	    
//	}// end while not done
///*	try {
//	    summary.close();
//	} catch (IOException e) {
//	    say("Problem closing summary.");
//	    e.printStackTrace();
//	}
//	try {
//	    texFile.write(Algorithms.TEX_FOOTER);
//	} catch (IOException e) {
//	    say("problem writing tex footer.");
//	    e.printStackTrace();
//	}
//	try {
//	    texFile.close();
//	} catch (IOException e) {
//	    say("Problem closing tex file.");
//	    e.printStackTrace();
//	}
//*/
//	System.out.println("Finished.");
//
//	
//    }// end main
//
//    public static int getRank() {
//        return rank;
//    }
//
//    public static void setRank(int rank) {
//        OldSectionIntegral.rank = rank;
//    }
//
//    public static Group getG() {
//        return g;
//    }
//
//    public static void setG(Group g) {
//        OldSectionIntegral.g = g;
//    }
//
//    public static String getName() {
//        return name;
//    }
//
//    public static void setName(String name) {
//        OldSectionIntegral.name = name;
//    }
//
//    public static Unipotent getU() {
//        return u;
//    }
//
//    public static void setU(Unipotent u) {
//        OldSectionIntegral.u = u;
//    }
//
//    public static Polynomial getP() {
//        return p;
//    }
//
//    public static void setP(Polynomial p) {
//        OldSectionIntegral.p = p;
//    }
//
//    public static int[][] getLeftInv() {
//        return leftInv;
//    }
//
//    public static void setLeftInv(int[][] leftInv) {
//        OldSectionIntegral.leftInv = leftInv;
//    }
//
//    public static SectionIntegralPiece getThisPiece() {
//        return thisPiece;
//    }
//
//    public static void setThisPiece(SectionIntegralPiece thisPiece) {
//        OldSectionIntegral.thisPiece = thisPiece;
//    }
//
//    public static String getNegativeExponentWarning() {
//        return NEGATIVE_EXPONENT_WARNING;
//    }
//
//    public static char[] getSetupOptions() {
//        return SETUP_OPTIONS;
//    }
//
//    public static char[] getMainMenuOptions() {
//        return MAIN_MENU_OPTIONS;
//    }
//
//    public static String getMainMenu() {
//        return MAIN_MENU;
//    }
//
//    public static String getUpdateMessage() {
//        return UPDATE_MESSAGE;
//    }
//
//    private static void dropRoots(Scanner scanner) throws BadSubstitutionException {
//	int[] nextRoot = Algorithms.getRoot(scanner, g);
//	while( nextRoot != null){
//	    dropRoot(nextRoot, scanner);
//	    nextRoot = Algorithms.getRoot(scanner, g);
//	}
//	
//    }
//
//    private static void dropRoot(int[] nextRoot, Scanner scanner) throws BadSubstitutionException {
//	p = u.dropRoot(nextRoot, p, scanner);
//	
//    }
//
//    /**
//     * Should check whether u, p and leftInv match the corresponding 
//     * attributes of thisPiece.  If they do not, then a piece with 
//     * these attributes should be saved, either by overwriting 
//     * thisPiece or by saving under a custom name.
//     * 
//     */
//    private static void update() {
//	if(!upToDate()){
//	    say(UPDATE_MESSAGE);
//	    //TODO FINISH
//	}
//    }
///**
// * Check whether u, p and leftInv match the corresponding 
// * attributes of thisPiece. 
// * 
// * @return true if they do and false if one or more does not.
// */
//    private static boolean upToDate() {
//	return u.equals(thisPiece.getUnip())
//	&&(p==thisPiece.getPoly())
//	&&Arrays.equals(leftInv, thisPiece.getLeftInv());
//    }
//
//    private static void iwasawa() {
//	    
//	
//	
//	    try {
//		if(Algorithms.containsValue(leftInv,u.getGroup().negative(u.getLast().getRoot())) ){
//		System.out.print("Iwasawa...\n");
//	    if( u.getLast().getArgument().getAbsoluteDegree()!= 1|| u.getLast().getArgument().getNumberOfTerms()!=1)
//		System.out.println("The rightmost Sunipotent has a complicated argument.  This program\n" +
//				"can not handle Iwasawa for such a situation (sorry).  \n" +
//				"Try reorder or substitute.\n\n");
//	    else
//	    {
///*		try {
//		    summary.write(currentPiece+" = "+ currentPiece+"0 + "+ currentPiece+"1\n");
//		} catch (IOException e) {
//		    say("Problem writing summary.");
//		    e.printStackTrace();
//		}*/
//		// identify the (unique) variable appearing in that last polynomial
//		String variable = u.getLast().getArgument().getVar();
//		// TODO getVar() is kind of inelegant.  Improve.
//		
//		// determine whether it appears in the psi factor and print alert if it does.
//		if(!p.isIndependentOf(variable))
//		    System.out.println("Alert.  Iwasawa on a variable which appears in the psi factor.\n");
//		// determine whether it appears in any terms other than the last and print alert if it does
//		for( int i=u.getNumberOfTerms() -2; i >-1 ; i--){
//		    if(!u.get(i).getArgument().isIndependentOf(variable))
//			System.out.print("Alert.  Iwasawa variable also appears in term "+ i + ":  "+u.get(i) + "\n");
//		    
//		}
//		// if variable was associated to a different Suni, print alert.
//		if(u.suniOf(variable) != u.getLast())
//		    System.out.println("Alert.  Iwasawa variable was associated to another Sunipotent.  Association deleted.\n");
//		// in any event, delete any associations involving this variable.
//		u.disassociate(variable);
//		    
//		
//		
//		
//		// manage the Piece list "pieces."
///*		int index = pieces.indexOf(currentPiece);
//		pieces.remove(index);
//		pieces.add(index, currentPiece+"0");
//		pieces.add(index, currentPiece+"1");
//*/		
//
//		// grab the root from the last Suni before removing it.
//		int[] a = u.getLast().getRoot();
//		
//		
//
//		
//		// save first piece:  variable is in \frak{o}:  simply drop the last term.
//		u.remove(u.getNumberOfTerms()-1);
//		
//		// manage domain: if variable from last Suni still appears somewhere else, 
//		// we have to keep track of it...
///*		if( thisPiece.isIndependentOf(variable))
//		    Piece.savePiece( name+"/"+currentPiece+"0.ser", new Piece(leftInv,  u , p, 
//			thisPiece.getTorusPart(), thisPiece.getJacobian(), thisPiece.getDomain()));
//		else{
//		    Map <Polynomial, String > newDomain = new HashMap<Polynomial, String>(thisPiece.getDomain());
//		    newDomain.put(Polynomial.create(variable), "\\in \\frak o");
//		    Piece.savePiece( name+"/"+currentPiece+"0.ser", new Piece(leftInv,  u , p, 
//				thisPiece.getTorusPart(), thisPiece.getJacobian(), newDomain));
//		}
//*/
//		// handle conjugation by the torus part from the Iwasawa
//		for( Sunipotent suni :u.getTerms()){
//		    
//		    // rule: x_r(p) . a^vee( t ) = a^vee(t) . x_r( p * t^{-<r, a^vee>})
//		    // here a^vee = coroot for root a, < , >  = natural pairing
//		    int [] r = suni.getRoot();
//		    suni.setArg(suni.getArgument().times(Monomial.create(variable, 
//			    (-2*u.getGroup().weylInvtInnProd(r, a))/u.getGroup().weylInvtInnProd(a, a))));
//		    // <r, a^vee> = 2(r,a)/(a,a) where (,) = Weyl invariant inner product.
//		    
//		    
//		    if( u.stringOf(suni)!= null){
//			// make changes of variable to simplify those sunis that are
//			// associated to certain variables:  y ->y *t^{<r, av>} (notation as above.)
//			// if previous step changed x_r(y) to x_r( y* t^{-<r, av>}), this changes it back.
//			p = p.replace(u.stringOf(suni), Polynomial.create(u.stringOf(suni)).times(
//				Monomial.create(variable, 
//				    (2*u.getGroup().weylInvtInnProd(r, a))/u.getGroup().weylInvtInnProd(a, a))
//				));
//			u.replace(u.stringOf(suni), Polynomial.create(u.stringOf(suni)).times(
//				Monomial.create(variable, 
//				    (2*u.getGroup().weylInvtInnProd(r, a))/u.getGroup().weylInvtInnProd(a, a))
//				));
//			
//			thisPiece.setJacobian(thisPiece.getJacobian().times(Monomial.create(variable, 
//				    2*u.getGroup().weylInvtInnProd(r, a)/u.getGroup().weylInvtInnProd(a, a))));
//			
//			
//		    }// end if stringOf != null
//		    
//		    
//		    
//		}// end iteration over sunis 
//		thisPiece.setTorusPart(thisPiece.getTorusPart().times(Toral.coroot(a, new RationalFunction(1,variable))));
//		
//		
//		
//		// introduce the upper unipotent part of the Iwasawa at right
//		// new root will be negative of root from original suni.
//		for( int i = 0 ; i < a.length; i++)
//		    a[i] *= -1;
//		u.rightMultiply(Sunipotent.create(a, Polynomial.create(variable)));
//		u.pushFarLeftAndRemove(u.getNumberOfTerms()-1);
//		u = edu.siu.math.egut.egut.Algorithms.l_reduce(u, leftInv);
//		p= u.chOfVars(p);
////		currentPiece = currentPiece +"1";
////		Piece.savePiece(name+"/"+currentPiece+".ser", thisPiece);
//		if(u.hasNegativeExponents())
//		    System.out.println(NEGATIVE_EXPONENT_WARNING);
//		
//		
//		/*TODO resolve the following fairly thorny issue:  
//		 * if the arguments of Sunis are permitted to be 
//		 * rational functions, then the business of making 
//		 * changes of variable to keep them simple could 
//		 * become fairly complicated.  
//		 * 
//		 * Example:  argument is r1
//		 * because of relations, it becomes 
//		 * r1+ p/q = (r1 q + p)/q
//		 * we try to put it back by doing
//		 * r1-> 2*r1- (r1 q + p)/ q = (2r1 q - r1 q - p)/q = (r1 q - p)/ q
//		 * making this sub in the term of interest, get
//		 * ((r1 q  -p) q +p q)/(q*q) or (q^2 r1)/q^2.
//		 * Clearly, this is r1, but training the machine to recognize that
//		 * will not be so easy, I think... D'oh.  
//		 */
//		
//		
//		
//		
//	    }// end "else" to if the last Suni has a nasty arg.		
//		}// end if -a is in the invRoots... TODO combine the two "can't Iwasawa scenarios."
//		else{
//		    System.out.print("The root of the last sunipotent does not appear in the list of left-invariant roots.\n" +
//		    		"this is probably a very serious error.   If you're sure its not, you can try to proceed using\n" +
//		    		"reorder to put some other sunipotent rightmost.\n");
//		    
//		}// end else
//		    }/*end try*/ catch (NotARootException e) {
//			
//			e.printStackTrace();
//			System.out.println("Serious error: int[] obtained using Sunipotent.getRoot() produced NotARootException.");
//			System.exit(1);
//		    }// end catch 
// catch (BadSubstitutionException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		    }
//	
//	    
//	
//    }
//
//    private static void customizeVars(Scanner scanner) throws BadSubstitutionException {
//	int i = 0;
//	    say(u.toStringWithIndices()
//		    + "\nSunipotent number? (-1 when done).");
//	    i = Algorithms.getInt(scanner);
//	while (i != -1) {
//	    if (u.get(i).getArgument().isAVariable()) {
//		String r = u.get(i).getArgument().getVar();
//		say("variable? (don't use z)");
//		String s = Algorithms.getVariable(scanner);
//		if( u.suniOf(r)!= null)
//		    u.associate(u.suniOf(r), s);
//		u.replace(r, Polynomial.create(s));
//		p=p.replace(r, Polynomial.create(s));
//		
//		/* make sub in other parts of the Piece?
//		 * 
//		 */
//		    say(u.toStringWithIndices()
//			    + "\nSunipotent number? (-1 when done).");
//		
//	    } else
//		say("Specified sunipotent does not have a single variable in its "
//			+ "argument.  Please try another.");
//	    i = Algorithms.getInt(scanner);
//
//	}
//
//	
//    }
//
//    private static void saveAs(Scanner scanner) {
//	    System.out.println("enter desired filename.");
//	    String customName = scanner.next();//TODO make more robust, add overwrite checking.
//	    SectionIntegralPiece.savePiece(customName+".ser", new SectionIntegralPiece( thisPiece.getLeftInv(),u,p, thisPiece.getTorusPart(), thisPiece.getJacobian(), thisPiece.getDomain()));
//	    
//	    
//	    System.out.println("A copy of the open piece in its current state has " +
//	    		"been saved to "+customName+".ser");
//	
//    }
//
//   public static void say(String string) {
//	System.out.println(string);
//    }
//
//    private static void initializeAuxFiles(Scanner scanner) throws IOException {
//	// TODO Auto-generated method stub
//	/*	System.out.println("Finally, please input a name for this \n" +
//	"integral.  (This should be a string.  When pieces\n" +
//	"of the integral are stored, the filenames will consist" +
//	"\n of this name and a numerical suffix.)");
//*/	name = "sectInt"+Calendar.getInstance().getTime().getDay()+Calendar.getInstance().getTime().getMonth()+"_"+Calendar.getInstance().getTime().getHours()+Calendar.getInstance().getTime().getMinutes();//scanner.next();
///*
//	directory = new File(name);
//	directory.mkdir();
//	    
//	summary = new FileWriter(name+"/summary");
//	texFile = new FileWriter( name+"/"+"finishedPieces.tex");
//	texFile.write(Algorithms.TEX_HEADER);
//	    
//	    
//	pieces.add(name);
//	currentPiece = name;
//*/
//	
//    }
//
//    private static SectionIntegralPiece getInitialDataAsUserInput(Scanner scanner)
//    {
//    
//	//Get the initial data.
//	System.out.println("Input the name of a file containing the desired data in the following order:\n " +
//			"\t left invariance roots (P . w)" +
//			"\t roots defining the group the integral is over (N)");
//	
//	String filename =  edu.siu.math.egut.io.Algorithms.getFileName(scanner);
//	// TODO handle case user bails on inputting this way.
//	String s;
//	try {
//	    s = Algorithms.fileToString(filename);
//	} catch (FileNotFoundException e) {
//	    s="[[1]],[[1]]";  // to ensure that s is always initialized before its use later... though this 
//	    // line would never be read...
//	}	int[][] leftInv=new int[1][1];
//	int[][] m = new int[1][1];
//	try {
//	    leftInv = Algorithms.readLiEMatrix(s);
//
//	    m = Algorithms.readLiEMatrix(s, s.indexOf("]]")+2);
//	} catch (Exception e) {
//	    System.out.println("Problem reading file.");
//	    e.printStackTrace();
//	    System.exit(1);
//	}
//	// initialize the constant "rank"
//	
//	
//	// set up the unipotent 
//	Unipotent u;
//	try {
//	    u = new Unipotent(m, "u");
//	} catch (NotARootException e) {
//	    // TODO Auto-generated catch block
//	    e.printStackTrace();
//	    u= new Unipotent();
//	    System.exit(1);
//	    
//	}
//	
//	// show the user the unipotent 
//	System.out.println(u);
//	
//	// get a polynomial (the psi factor)
//	
//	//TODO restore
///*	Polynomial p = Polynomial.create(1);
//	try {
//	    p = Polynomial.read("u1+u2+u4");
//	} catch (PolynomialReadException e1) {
//	    // TODO Auto-generated catch block
//	    e1.printStackTrace();
//	}
//*/	
//	System.out
//		.println("Next, please input a polynomial in the variables corresponding to the roots "
//			+ "from the second matrix as above.");
//
//	Polynomial p = edu.siu.math.egut.io.Algorithms.getPoly(scanner);
//	
//	return new SectionIntegralPiece(leftInv,u, p);
//	
//}
//
//    
//    private static SectionIntegralPiece getSetUp( Scanner scanner, String[] args) {
//	SectionIntegralPiece thisPiece = null;
//	while(thisPiece == null){
//
//	System.out.println( "[R]ead new setup \t [O]pen old setup");
//	char setUpType =  Algorithms.getMenuAnswer(SETUP_OPTIONS, scanner);
//
//	if(setUpType == 'R'||setUpType == 'r'){
//	    thisPiece = getInitialDataAsUserInput(scanner);
//	}
//	else{
//	    System.out.println("Filename (include \".ser\")");
//	    
//	    thisPiece = Algorithms.getSavedPiece(scanner, null);
//	    }
//	    
//	}
//	
//	
//	return thisPiece;
//	// TODO implement setup via args...
//	
//    }
//
//}// end class declaration
